//-----------------------------------------------------------------------
// <copyright file="a_PrizeDAOBase.cs" company="AACECOM">
//     Copyright (c) AACECOM. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Data;
using System.Data.Common;
using System.Collections;
using Microsoft.Practices.EnterpriseLibrary.Data;
using ThoiTrang.Entities;

namespace ThoiTrang.Data
{    
    public abstract class a_PrizeDAOBase
    {
        #region Common methods
        public virtual a_Prize Createa_PrizeFromReader(IDataReader reader)
        {
            a_Prize item = new a_Prize();
            try
            {
                if (!reader.IsDBNull(reader.GetOrdinal("PrizeID"))) item.PrizeID = (int)reader["PrizeID"];
                if (!reader.IsDBNull(reader.GetOrdinal("StartDate"))) item.StartDate = (DateTime)reader["StartDate"];
                if (!reader.IsDBNull(reader.GetOrdinal("EndDate"))) item.EndDate = (DateTime)reader["EndDate"];
                if (!reader.IsDBNull(reader.GetOrdinal("NumPrize"))) item.NumPrize = (int)reader["NumPrize"];
                if (!reader.IsDBNull(reader.GetOrdinal("DomainID"))) item.DomainID = (int)reader["DomainID"];
                if (!reader.IsDBNull(reader.GetOrdinal("Status"))) item.Status = (bool)reader["Status"];
            }
            catch (Exception ex)
            {                
                throw ex;
            }
            return item;
        }
        #endregion
        
        #region Createa_Prize methods
            
        public virtual void Createa_Prize(a_Prize a_Prize)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_Prizes_ins");
                
                database.AddInParameter(dbCommand, "@StartDate", DbType.DateTime, a_Prize.StartDate);
                database.AddInParameter(dbCommand, "@EndDate", DbType.DateTime, a_Prize.EndDate);
                database.AddInParameter(dbCommand, "@NumPrize", DbType.Int32, a_Prize.NumPrize);
                database.AddInParameter(dbCommand, "@DomainID", DbType.Int32, a_Prize.DomainID);
                database.AddInParameter(dbCommand, "@Status", DbType.Boolean, a_Prize.Status);
                database.AddOutParameter(dbCommand, "@PrizeID", DbType.Int32, 0);
                
                database.ExecuteNonQuery(dbCommand);
                a_Prize.PrizeID = (int)database.GetParameterValue(dbCommand, "@PrizeID");
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion

        #region Updatea_Prize methods
        
        public virtual void Updatea_Prize(a_Prize a_Prize)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_Prizes_upd");            
                
                database.AddInParameter(dbCommand, "@PrizeID", DbType.Int32, a_Prize.PrizeID);
                database.AddInParameter(dbCommand, "@StartDate", DbType.DateTime, a_Prize.StartDate);
                database.AddInParameter(dbCommand, "@EndDate", DbType.DateTime, a_Prize.EndDate);
                database.AddInParameter(dbCommand, "@NumPrize", DbType.Int32, a_Prize.NumPrize);
                database.AddInParameter(dbCommand, "@DomainID", DbType.Int32, a_Prize.DomainID);
                database.AddInParameter(dbCommand, "@Status", DbType.Boolean, a_Prize.Status);
                
                database.ExecuteNonQuery(dbCommand);
            }
            catch (Exception ex)
            {
               throw ex;
            }
        }
        
        #endregion

        #region Deletea_Prize methods
        public virtual void Deletea_Prize(int prizeID)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_Prizes_del");
                
                database.AddInParameter(dbCommand, "@PrizeID", DbType.Int32, prizeID);
                
                database.ExecuteNonQuery(dbCommand);
            }
            catch (Exception ex)
            {
               throw ex;
            }
        }

        #endregion

        #region Geta_Prize methods
        
        public virtual a_Prize Geta_Prize(int prizeID)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_Prizes_sel");
                
                database.AddInParameter(dbCommand, "@PrizeID", DbType.Int32, prizeID);
                
                a_Prize a_Prize =  null;
                using (IDataReader reader = database.ExecuteReader(dbCommand))
                {
                    if (reader.Read())
                    {
                        a_Prize = Createa_PrizeFromReader(reader);
                        reader.Close();
                    }
                }
                return a_Prize;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        
        #endregion

        #region Geta_PrizeList methods
        public virtual a_PrizeCollection Geta_PrizeList(a_PrizeColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_Prizes_selList");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                a_PrizeCollection a_PrizeCollection = new a_PrizeCollection();
                using (IDataReader reader = database.ExecuteReader(dbCommand))
                {
                    while (reader.Read())
                    {
                        a_Prize a_Prize = Createa_PrizeFromReader(reader);
                        a_PrizeCollection.Add(a_Prize);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return a_PrizeCollection;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        
        public virtual a_PrizeCollection Geta_PrizeList(a_PrizeColumns orderBy, OrderDirection orderDirection)
        {            
            int totalRecords = 0;
            return Geta_PrizeList(orderBy, orderDirection, 0, 0, out totalRecords);
        }
        
        #endregion
    }
}